// component/containner/containner.js
const
    utils = require('../../utils/util'),
    globalData = getApp().globalData;

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        status: {
            type: Number, //0:加载中，1:超时，2：正常 3 文章删除无按钮 4 文章删除有按钮 5 回答删除有按钮 6. 无网络
            value: 3,
        },
	    homeBottom: { //home按钮距离页面底部距离
            type: Number,
            value: 180,
        },
        tips: {
            type: String,
            value: ''
        },
        btnText:{
            type: String,
            value: ''
        }
    },

    async attached() {
        let pages = getCurrentPages(),
            showHome = false,
            currentRoute = pages[0].route,
            ignore = [
                'pages/center/center',
                'pages/index/index',
                'pages/recommend/recommend',
                'pages/service/index',
                'sub_third/pages/live/room/room',
                'pages/vipMall/vipMall'
            ];

        showHome = !ignore.includes(currentRoute);

        // const noNetworkImage =  await this.preloadImage('status6Image', this.data.imageBaseUri + '/v3.0.2/status-6.png')

        this.setData({
            showHome,
            // 预加载无网络图片
            // 'defaultImages.1': noNetworkImage,
            // 'defaultImages.6': noNetworkImage
        });
    },

    /**
     * 组件的初始数据
     */
    data: {
        imageBaseUri: globalData.imageBaseUri,
        showHome: false,
        toast: {
            visible: false,
            text: ''
        },
        defaultTips: {
            1: '等的太久,还是没反应！',
            6: '哎呀，网络好像开小差了~'
        },
        // defaultImages: {
        //     1: globalData.imageBaseUri + '/v3.0.2/status-6.png',
        //     3: globalData.imageBaseUri + '/v3.0.2/status-3.png',
        //     6: globalData.imageBaseUri + '/v3.0.2/status-6.png'
        // }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 预加载图片
        preloadImage(key, url) {
            return new Promise((resolve, reject) => {
                const image = wx.getStorageSync(key)
                if (image) {
                    resolve(image)
                } else {
                    wx.downloadFile({
                        url,
                        success(res) {
                            const tempFilePath = res.tempFilePath
                            wx.setStorageSync(key, tempFilePath)
                            resolve(tempFilePath)
                        },
                        fail: reject
                    })
                }
            })
        },
        //请求超时，向上通知重新请求；
        reload() {
            console.log(getApp().globalData.networkType, globalData.networkType)
            if (globalData.networkType !== 'none') {
                this.triggerEvent("reload");
            } else {
                this.setData({
                    'toast.visible': true,
                    'toast.text': '请连接网络'
                })
            }
        },

        //返回首页
        goHome() {
            let pages = getCurrentPages(),
                p = pages[pages.length - 1],
                route = p.route;

            //在登录页点击home按钮进入其他页面后，‘分享后登录，跳转至分享页面’功能无效
            if (route === 'pages/login/login') {
                globalData.returnUrl = '';
            }

            wx.reLaunch({
                url: globalData.homeUri
            })
        },
    }
})